package com.deparse.dpframe.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by hsy on 2016/4/15.
 */
public class DPImageUtil extends DPFileUtil {

    private static final int DEFAULT_MAX_WIDTH = 960;
    private static final int DEFAULT_MAX_HEIGHT = 640;
    private static final int DEFAULT_MAX_SIZE = 200 * 1024;
    private static final int DEFAULT_MIN_QUALITY = 50;

    public static Bitmap getBitmapFromFile(String srcPath) {
        return getBitmapFromFile(srcPath, DEFAULT_MAX_WIDTH, DEFAULT_MAX_HEIGHT);
    }

    public static Bitmap getBitmapFromFile(String srcPath, int maxWidth, int maxHeight) {
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        newOpts.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(srcPath, newOpts);//此时返回bm为空

        int w = newOpts.outWidth;
        int h = newOpts.outHeight;

        float hh = maxHeight;
        float ww = maxWidth;
        //缩放比。由于是固定比例缩放，只用高或者宽其中一个数据进行计算即可
        int be = 1;//be=1表示不缩放
        if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放
            be = (int) (newOpts.outWidth / ww);
        } else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放
            be = (int) (newOpts.outHeight / hh);
        }
        if (be <= 0)
            be = 1;
        newOpts.inSampleSize = be;//设置缩放比例
        //重新读入图片，注意此时已经把options.inJustDecodeBounds 设回false了
        newOpts.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(srcPath, newOpts);
    }

    public static void saveBitmapToFile(Bitmap bitmap, File file) {
        saveBitmapToFile(bitmap, file.getAbsolutePath());
    }

    public static File saveBitmapToFile(Bitmap bitmap, String path) {
        File file = DPFileUtil.makeFile(path);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
            return file;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != fos) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static Bitmap compressBySizeAndQuality(Bitmap source) {
        return compressBySizeAndQuality(source, DEFAULT_MAX_SIZE);
    }

    public static Bitmap compressBySizeAndQuality(Bitmap source, int maxSize) {
        return compressBySizeAndQuality(source, maxSize, DEFAULT_MIN_QUALITY);
    }

    public static Bitmap compressBySizeAndQuality(Bitmap source, int maxSize, int minQuality) {
        DPLogUtil.enter("");
        if (null == source || source.isRecycled()) {
            return null;
        }
        int quality = 90;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        source.compress(Bitmap.CompressFormat.JPEG, quality, bos);
        while (bos.toByteArray().length > maxSize && quality > minQuality) {
            quality -= 10;
            bos.reset();
            source.compress(Bitmap.CompressFormat.JPEG, quality, bos);
        }
        Bitmap bitmap = BitmapFactory.decodeByteArray(bos.toByteArray(), 0, bos.toByteArray().length);
        if (null != source && !source.isRecycled()) {
            source.recycle();
        }
        return bitmap;
    }

    public static Bitmap compressBySizeAndQuality(String srcPath) {
        return compressBySizeAndQuality(getBitmapFromFile(srcPath));
    }
}
